
$(function(){
	//购物车 显示与隐藏
	var shopcar=$('#shop-car');
             	var shopcarinfo=$('#shop-car-info')
             	var shopcarlist=$('#shop-car-list')
             	shopcar.mouseover(function(){
             		shopcarinfo.addClass('hover');
             		shopcarlist.css('display','block')
             	}).mouseout(function(){
             		shopcarinfo.addClass('hover');
             		shopcarlist.css('display','none')
             	})
	
	// 导航的菜单栏的隐藏与显示
	
	var
			oTimer = null, 
			oShowNav = $('#nav-menu-wrap');
		$('#ull').mouseenter(function () {
			clearInterval(oTimer);
			oShowNav.css({display:'block'}).stop().animate({
							height: 234,
						}, 500);
		}).mouseleave(function () {
			oTimer = setTimeout( function () {
				oShowNav.stop().animate({
					height:0
				}, 500, function () {
					oShowNav.css('display', 'none');
				});
			}, 50);
		});
		$('#ull li').mouseover(function () {
			var iIndex = $(this).index();
			$('#nav-menu ul').css('display', 'none').eq(iIndex).css('display', 'block');
		});

		oShowNav.mouseenter(function () {
			clearInterval(oTimer);
		}).mouseleave(function () {
			console.log(1)
			oTimer = setTimeout( function () {
				oShowNav.stop().animate({
					height:0
				}, 500, function () {
					oShowNav.css('display', 'none');
				});
			}, 50);
		})
	
	// 搜索框以及跨域
	var oTex=$('#tex'),
		oUll02=$('#ull02'),
		oSeachNotice=$('#search-notice'),
		iSearchIndex=-1,
		sOriHtml= oUll02.html();
		//console.log(1)
		oTex.focus(function(){
			console.log(2)
			oTex.addClass('search-box-hover');
			oSeachNotice.fadeOut('fast');
			oUll02.css('display', 'block');
			console.log(3)
		}).blur(function () {
			oTex.removeClass('search-box-hover');
			oSeachNotice.fadeIn('fast');
			oUll02.css('display', 'none');
		}).keyup(function (e) {
			var
				e = e || window.event,
				aLi = $('#ull02 li');
			if(e.keyCode === 38 || e.keyCode === 40) {
				if(e.keyCode === 38 && iSearchIndex > 0) {
					iSearchIndex--;
				} else if(e.keyCode === 40 && iSearchIndex < aLi.length - 1) {
					iSearchIndex++;
				}
				oTex.val(aLi.removeClass('active').eq(iSearchIndex).addClass('active').data('key'));
			}
		}).bind('input propertychange', function () {
			var sSearchCon = $(this).val();
			if(sSearchCon) {		
				$.ajax({
					url:'http://search.mi.com/search/expand',					
					data:{keyword:sSearchCon},					
					jsonp: 'jsonpcallback',
					dataType:'jsonp',
					success:function (data) {
									var sHtml = '';
									data.forEach(function (v) {
										sHtml += '<li data-key="' + v.Key + '"><a href="javascript:;"><span>约有' + v.Rst + '件</span>' + v.Key + '</a></li>';
									});
									if(sHtml) {
										oUll02.css('display', 'block').html(sHtml);
									} else {
										oUll02.css('display', 'none');
									}
									iSearchIndex = -1;
								},
							});
				}else{
					oUll02.html(sOriHtml);
					iSearchIndex = -1;
				}
					
			})
	
		// 左右 移动
		var
			oShowClist = $('#c-list');
		$('#ul01').mouseenter(function () {
			clearInterval(oTimer);
			oShowClist.css({display:'block'}).stop().animate({
							width: 500,
						}, 500);
		}).mouseleave(function () {
			oTimer = setTimeout( function () {
				oShowClist.stop().animate({
					width:0
				}, 500, function () {
					oShowClist.css('display', 'none');
				});
			}, 50);
		});
		$('#ul01 li').mouseover(function () {
			var iIndex = $(this).index();
			$('#c-list-center ul').css('display', 'none').eq(iIndex).css('display', 'block');
		});

		oShowClist.mouseenter(function () {
			clearInterval(oTimer);
		}).mouseleave(function () {
			oTimer = setTimeout( function () {
				oShowClist.stop().animate({
					width:0
				}, 500, function () {
					oShowClist.css('display', 'none');
				});
			}, 50);
		})
	
	// 轮播图
		var
			aBtnA = $('#btn-list a'),
			aBannerImg = $('#ul02 img'),
			iIndex = 0,
			oBannerTime = null;

		// 点击按钮
		aBtnA.click(function () {
			iIndex = $(this).index();
			console.log(iIndex)
			bannerMove(iIndex);
		});

		// 往左运动
		$('#pre').click(function() {
			iIndex--;
			if(iIndex < 0) {
				iIndex = aBannerImg.length - 1;
			}
			bannerMove(iIndex);
		}).mouseover(function(){
			$('#pre').css({opacity:1})
		}).mouseout(function(){
			$('#pre').css({opacity:0.1})
		});

		// 往右运动
		$('#next').click(function() {
			iIndex++;
			if(iIndex >= aBannerImg.length) {
				iIndex = 0;
			}
			bannerMove(iIndex);
		}).mouseover(function(){
			$('#next').css({opacity:1})
		}).mouseout(function(){
			$('#next').css({opacity:0.1})
		})

		// 清除定时器
		$('#div').hover(function () {
			clearInterval(oBannerTime);
		}, function () {
			autoMove();
		});

		// 自动轮播
		autoMove();

		function autoMove() {
			oBannerTime = setInterval(function () {
				iIndex++;
				if(iIndex >= aBannerImg.length) {
					iIndex = 0;
				}
				bannerMove(iIndex);
			}, 1000);
		}

		function bannerMove(iIndex){
			aBtnA.removeClass('active').eq(iIndex).addClass('active');
			aBannerImg.stop(true).animate({opacity:0}, 500, function () {
				$(this).css('display', 'none');
				//console.log(1)
			}).eq(iIndex).css('display', 'block').stop(true).animate({opacity:1}, 500);
		} 
	
	
	
	
	
	
})





